<template>
	<view class="setPassword">
		<u-navbar back-icon-name="close" back-icon-size="30" :border-bottom="false" :background="{background:'#f7f7f7'}" ></u-navbar>
		<view class="title-warp">
			<view class="title">{{auth.title}}</view>
			<view class="subtitle">{{auth.subTitle}}</view>
		</view>
		<u-message-input :focus="true" :maxlength="6" dot-fill @finish="finish" v-model="password"></u-message-input>
		<!-- <authComponent ref="authComponent" :title="auth.title" :subTitle="auth.subTitle" @submit="submit" @close="close"></authComponent> -->
		<u-no-network></u-no-network>
	</view>
</template>

<script>
	let that
	import {
		mapGetters,
		mapActions
	} from 'vuex'
	export default {
		computed:{
			...mapGetters(['userInfo', 'api']),
		},
		data() {
			return {
				password:'',
			}
		},
		onLoad(e) {
			that = this
			that.auth={
				title:e.title,
				subTitle:e.subTitle
			}
		},
		onShow() {
		},
		methods: {
			...mapActions(['set_userInfo']),
			init(){
				that.password = '666666';
				setTimeout(()=>{
					that.password = '';
				},100)
			},
			setPassword(value){
				let content=`确认设置密码为${value}？`
				that.password=value
				uni.showModal({title: '再次确认',content: content,cancelText: '重试',confirmText: '确定',
					success: res => {
						if(res.confirm){
							that.create()
						}else{
							that.init()
						}
					}
				});
			},
			create(){
				this.$u.post(that.api.GLOBAL_sysUserCert_create, {certSecretKey:that.password}).then(res => {
					if (res.code == 200) {
						uni.navigateBack({
							delta: 1
						});
						setTimeout(()=>{
							uni.showToast({
								title: res.msg,
								icon:"none",
								duration: 2000
							});
						},500)
						let userinfo=JSON.parse(JSON.stringify(that.userInfo))
						userinfo.hasCertKey=true
						that.set_userInfo(userinfo);
					} 
				});
			},
			edit(value){
				this.$u.post(that.api.GLOBAL_sysUserCert_edit, {certSecretKey:value}).then(res => {
					if (res.code == 200) {
						uni.navigateBack({
							delta: 1
						});
						setTimeout(()=>{
							uni.showToast({
								title: res.msg,
								icon:"none",
								duration: 2000
							});
						},500)
					} 
				});
			},
			finish(value){
				if(that.auth.title=='设置认证密码'){
					that.setPassword(value)
				}else if(that.auth.title=='修改认证密码'){
					that.edit(value)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.setPassword {
		background-color: #f7f7f7;width: 100%;height: 100%;
		.title-warp{
			text-align: center;font-size: 30rpx;line-height: 70rpx;padding: 100rpx 0;
			.title{
				font-size: 40rpx;font-weight: bold;
			}
		}
	}
</style>
